define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/report/models'),
        op = require('js/apps/op/models'),
        utils = require('js/utils/tools'),
        moment = require('moment'),
        chart = require('js/utils/chart'),
        helper = require('js/apps/report/common'),
        cookie = require('js/utils/cookie');

    require('datetimepicker');
    require('multiselect');
    require('magnific');
    require('jquery-ui');

    var MetisBullView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
            'click #autoPoolBtn': 'doAutoPool',
            'click #autoRateBtn': 'doAutoRate',
            'click #closePoolBtn': 'closePoolDialog',
            'click #savePoolBtn': 'setCurrentPool',
            'click #closeRateBtn': 'closeRateDialog',
            'click #saveRateBtn': 'setCurrentRate'
        },
        initialize: function (options) {
            this.options = options;
            this.myDate = new Date();
            this.model = new models.MetisTigerReport();
            this.model.bind('change reset', this.renderWithData, this);
        },
        Date2Str: function (x, y) {
            var z = { M: x.getMonth() + 1, d: x.getDate(), h: x.getHours(), m: x.getMinutes(), s: x.getSeconds() };
            y = y.replace(/(M+|d+|h+|m+|s+)/g, function (v) { return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-2) });
            return y.replace(/(y+)/g, function (v) { return x.getFullYear().toString().slice(-v.length) });
        },
        doAutoPool: function () {
            this.getCurrentPool();
        },
        doAutoRate: function () {
            this.getCurrentRate();
        },

        closePoolDialog: function () {
            $("#controlPool").val('');
        },
        closeRateDialog: function () {
            $("#controlRate").val('');
        },
        getCurrentPool: function () {
            var that = this;
            $.ajax({
                url: '/admin/metis_console/',
                type: 'GET',
                headers: {
                    'X-AUTH-USER': cookie.getCookie('bigbang_user_id'),
                    'X-AUTH-TOKEN': cookie.getCookie('bigbang_user_token'),
                    'content-type': 'application/json',
                },
                data: {
                    'game_id': 5503,
                    'date': this.Date2Str(this.myDate, "yyyy-MM-dd"),
                }

            }).done(function (resp) {
                var now_pool = resp.data.now_pool;
                setTimeout(() => {
                    $("#currentPool").text(now_pool)
                }, 500);;

            }).fail(function (resp) {
                console.log(JSON.stringify(resp));
            });
        },
        setCurrentPool: function () {
            var that = this;

            $.ajax({
                url: '/admin/metis_console/',
                type: 'POST',
                headers: {
                    'X-AUTH-USER': cookie.getCookie('bigbang_user_id'),
                    'X-AUTH-TOKEN': cookie.getCookie('bigbang_user_token')
                },
                data: JSON.stringify({
                    'control_pool': $("#controlPool").val(),
                    'game_id': 5503
                })
            }).done(function (resp) {
                notify.success('提示', '更新成功！');
                console.log(JSON.stringify(resp));
                $("#currentPool").text(resp.data.now_pool);
                that.closePoolDialog();

            }).fail(function (resp) {
                notify.error('错误', '更新失败！');
                console.log(JSON.stringify(resp));
                that.closePoolDialog();
            });

        },
        getCurrentRate: function () {
            var that = this;
            $.ajax({
                url: '/admin/metis_console/',
                type: 'GET',
                headers: {
                    'X-AUTH-USER': cookie.getCookie('bigbang_user_id'),
                    'X-AUTH-TOKEN': cookie.getCookie('bigbang_user_token'),
                    'content-type': 'application/json',
                },
                data: {
                    'game_id': 5503,
                    'date': this.Date2Str(this.myDate, "yyyy-MM-dd"),
                }

            }).done(function (resp) {
                var now_rate = resp.data.now_rate;
                setTimeout(() => {$("#currentRate").text(now_rate*100)}, 500);
            }).fail(function (resp) {
                console.log(JSON.stringify(resp));
            });
        },
        setCurrentRate: function () {
            var that = this;

            $.ajax({
                url: '/admin/metis_console/',
                type: 'POST',
                headers: {
                    'X-AUTH-USER': cookie.getCookie('bigbang_user_id'),
                    'X-AUTH-TOKEN': cookie.getCookie('bigbang_user_token')
                },
                data: JSON.stringify({
                    'control_rate': $("#controlRate").val(),
                    'game_id': 5503,
                })
            }).done(function (resp) {
                notify.success('提示', '更新成功！');
                console.log(JSON.stringify(resp));
                $("#currentRate").text(resp.data.now_rate*100);
                that.closeRateDialog();

            }).fail(function (resp) {
                notify.error('错误', '更新失败！');
                console.log(JSON.stringify(resp));
                that.closeRateDialog();
            });

        },
        doSearch: function () {
            this.options = {};
            this.options.date_range = helper.getDateRange();

            this.load();
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.model.list,
                options: this.options,
            }));
            helper.loadDatePicker({
                dateRange: this.options.date_range
            });
            var that = this;

            $('#autoPoolBtn').magnificPopup({
                items: {
                    src: '#autoPoolBtnPanel',
                    type: 'inline'
                },
                midClick: true,
                closeOnBgClick: false,
                callbacks: {
                    beforeOpen: function () {
                        $('#closePoolBtn').off('click').click(function () {
                            $.magnificPopup.close();
                        });

                        $('#savePoolBtn').off('click').click(function () {
                            that.setCurrentPool();
                        });
                    }
                }
            });
            $('#autoRateBtn').magnificPopup({
                items: {
                    src: '#autoRateBtnPanel',
                    type: 'inline'
                },
                midClick: true,
                closeOnBgClick: false,
                callbacks: {
                    beforeOpen: function () {
                        $('#closeRateBtn').off('click').click(function () {
                            $.magnificPopup.close();
                        });

                        $('#saveRateBtn').off('click').click(function () {
                            that.setCurrentRate();
                        });
                    }
                }
            });
        },

        load: function () {
            this.options.game_id = 5503;
            this.model.fetch({
                reset: true,
                data: {
                    game_id: this.options.game_id,
                    date_range: this.options.date_range.join(',')
                },
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "report/MetisTiger.html",
        render: function () {
            var today = moment().format('YYYY-MM-DD'),
                dateRange = [today, today],
                view = new MetisBullView({
                    date_range: dateRange,
                });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });
});
